﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UserControls_BatBase
{
    public class BatBase : Control
    {

        public event PaintEventHandler OnPainted;


        private Color m_BatBaseColor = Color.FromArgb(255, 77, 59);


        [Description("电量颜色"), Category("自定义")]
        public Color BatBaseColor
        {
            get { return m_BatBaseColor; }
            set { m_BatBaseColor = value; }
        }

        private bool m_BatBaseState = true;

        [Description("电量状态"), Category("自定义")]
        public bool BatBaseState
        {
            get { return m_BatBaseState; }
            set { m_BatBaseState = value; }
        }

        private int m_BatBaseWidth = 200;

        [Description("电量动态宽度"), Category("自定义")]
        public int BatBaseWidth
        {
            get { return m_BatBaseWidth; }
            set
            {
                m_BatBaseWidth = value;
                m_BatBaseWidth = m_BatBaseWidth / 10 * 10;
                intLeftX = value * -1;
            }
        }
        private int m_BatBaseHeight = 30;

        [Description("电量动态高"), Category("自定义")]
        public int BatBaseHeight
        {
            get { return m_BatBaseHeight; }
            set { m_BatBaseHeight = value; }
        }

        private int m_BatBaseSleep = 50;

        [Description("运行速度（毫秒）"), Category("自定义")]
        public int BatBaseSleep
        {
            get { return m_BatBaseSleep; }
            set
            {
                if (value <= 0)
                    return;
                m_BatBaseSleep = value;
                if (timer != null)
                {
                    timer.Enabled = false;
                    timer.Interval = value;
                    timer.Enabled = true;
                }
            }
        }
        private int m_chargebubbleSpeed = 0;

        [Description("充电气泡速度（0 disEN）"), Category("自定义")]
        public int chargeBubbleSpeed
        {
            get { return m_chargebubbleSpeed; }
            set { m_chargebubbleSpeed = value; }
        }

        private int m_chargebubbleIntervalTime = 500;

        [Description("充电气泡生成间隔时间(毫秒)"), Category("自定义")]
        public int chargeBubbleIntervalTime
        {
            get { return m_chargebubbleIntervalTime; }
            set { m_chargebubbleIntervalTime = value; }
        }

        private int lastchargeBubbleTime = 0;

        private List<Bubble> m_lstchargeBubble = new List<Bubble>();
        /// <summary>
        /// The timer
        /// </summary>
        Timer timer = new Timer();
        /// <summary>
        /// The int left x
        /// </summary>
        int intLeftX = -200;
        /// <summary>
        /// Initializes a new instance of the <see cref="UCBatBase" /> class.
        /// </summary>
        public BatBase()
        {
            this.Size = new Size(600, 100);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            timer.Interval = m_BatBaseSleep;
            timer.Tick += timer_Tick;
            this.VisibleChanged += UCBatBase_VisibleChanged;
        }

        /// <summary>
        /// Handles the VisibleChanged event of the UCBatBase control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void UCBatBase_VisibleChanged(object sender, EventArgs e)
        {
             timer.Enabled = this.Visible;
        }

        /// <summary>
        /// Handles the Tick event of the timer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void timer_Tick(object sender, EventArgs e)
        { 
            intLeftX -= 10;
            if (intLeftX == m_BatBaseWidth * -2)
                intLeftX = m_BatBaseWidth * -1;
           
            if (m_chargebubbleSpeed > 0)
            {
                lastchargeBubbleTime += m_BatBaseSleep;
                if (lastchargeBubbleTime >= m_chargebubbleIntervalTime)
                {
                    lastchargeBubbleTime = 0;
                    var r = new Random(DateTime.Now.Millisecond);
                    var b_x = r.Next(0, this.Width);
                    var b = new Bubble();
                    b.Key = Guid.NewGuid().ToString();
                    b.BubblePoint = new Point(b_x, 0);
                    b.BubbleWidth = 5;
                    m_lstchargeBubble.Add(b);
                }
            }
            else
            {
                m_lstchargeBubble.Clear();
            }
            this.Refresh();
        }
        /// <summary>
        /// Handles the <see cref="E:Paint" /> event.
        /// </summary>
        /// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            //g.SetGDIHigh();
            List<Point> lst1 = new List<Point>();
            List<Point> lst2 = new List<Point>();
            int m_intX = intLeftX;
            while (true)
            {
                lst1.Add(new Point(m_intX, 1));
                lst1.Add(new Point(m_intX + m_BatBaseWidth / 2, m_BatBaseHeight));

                lst2.Add(new Point(m_intX + m_BatBaseWidth / 2, 1));
                lst2.Add(new Point(m_intX + m_BatBaseWidth / 2 + m_BatBaseWidth / 2, m_BatBaseHeight));
                m_intX += m_BatBaseWidth;
                if (m_intX > this.Width + m_BatBaseWidth)
                    break;
            }
            //新建封闭图形
            GraphicsPath path1 = new GraphicsPath();
            if (m_BatBaseState == true)
                path1.AddCurve(lst1.ToArray(), 0.5F);
            else
                path1.AddLine(-1, -1, this.Width + 1, -1);

            path1.AddLine(this.Width + 1, -1, this.Width + 1, this.Height);
            path1.AddLine(this.Width + 1, this.Height, -1, this.Height);
            path1.AddLine(-1, this.Height, -1, -1);
            //新建封闭图形
            GraphicsPath path2 = new GraphicsPath();
            if (m_BatBaseState == true)//水波纹
                path2.AddCurve(lst2.ToArray(), 0.5F);
            else//矩形
                path1.AddLine(-1, -1, this.Width + 1, -1);

            path2.AddLine(this.Width + 1, -1, this.Width + 1, this.Height);
            path2.AddLine(this.Width + 1, this.Height, -1, this.Height);
            path2.AddLine(-1, this.Height, -1, -1);
            //填充封闭图形
            g.FillPath(new SolidBrush(Color.FromArgb(220, m_BatBaseColor.R, m_BatBaseColor.G, m_BatBaseColor.B)), path1);
            g.FillPath(new SolidBrush(Color.FromArgb(220, m_BatBaseColor.R, m_BatBaseColor.G, m_BatBaseColor.B)), path2);
            //气泡
            foreach (var item in m_lstchargeBubble)
            {
                GraphicsPath gp = new GraphicsPath();
                Rectangle rec = new Rectangle(item.BubblePoint.X - item.BubbleWidth / 2, this.Height - (item.BubblePoint.Y - item.BubbleWidth / 2), item.BubbleWidth / 3 * 2, item.BubbleWidth / 3 * 2);
                gp.AddEllipse(rec);

                Color[] surroundColor = new Color[] { Color.FromArgb(50,Color.White) };
                PathGradientBrush pb = new PathGradientBrush(gp);
                pb.CenterColor = Color.FromArgb(150, Color.White);
                pb.SurroundColors = surroundColor;
                g.FillPath(pb, gp);

                item.BubblePoint = new Point(item.BubblePoint.X, item.BubblePoint.Y + m_chargebubbleSpeed);
                item.BubbleWidth += 1;
            }

            if (OnPainted != null)
            {
                OnPainted(this, e);
            }
        }
        internal class Bubble
        {
            internal string Key { get; set; }
            /// <summary>
            /// 坐标
            /// </summary>
            internal Point BubblePoint { get; set; }
            /// <summary>
            /// 宽度
            /// </summary>
            internal int BubbleWidth { get; set; }
        }
    }
}
